<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
        <el-form-item label="反馈标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入反馈标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="反馈状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择反馈状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_feedback_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:feedback:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:feedback:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:feedback:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        :loading="loading"
        resizable
        row-key
        ref="tableRef"
        :row-config="{isHover: true}"
        :data="feedbackList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full'"
      >
        <vxe-column type="checkbox" align="center" min-width="60"/>
        <vxe-column title="反馈标题" field="title" align="center" min-width="100" show-overflow/>
        <vxe-column title="反馈内容" type="html" field="content" align="center" min-width="150" show-overflow/>
        <vxe-column title="反馈状态" field="status" align="center" min-width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_feedback_status" :value="scope.row.status"/>
          </template>
        </vxe-column>
        <vxe-column title="回复" type="html" field="reply" align="center" min-width="150" show-overflow/>
        <vxe-column title="管理员名" field="adminName" align="center" min-width="100"/>
        <vxe-column title="用户名" field="userName" align="center" min-width="100"/>
        <vxe-column title="备注" field="remark" align="center" min-width="100"/>
        <vxe-column title="操作" align="center" fixed="right" min-width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleFeedBack(scope.row)"
              v-hasPermi="['system:feedback:edit']"
            >处理</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:feedback:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:feedback:remove']"
            >删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改用户反馈对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="反馈标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入反馈标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提交用户" prop="userId">
              <el-select v-model="form.userId" placeholder="请输入选择用户" clearable>
                <el-option
                  v-for="(item, index) in userOptions"
                  :key="index"
                  :value="item.id"
                  :label="item.userName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="反馈状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_feedback_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="反馈内容">
              <editor-comment v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回复" prop="reply">
              <editor-comment v-model="form.reply" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 处理弹框 -->
    <el-dialog title="处理反馈" :visible.sync="openHandle" v-if="openHandle" append-to-body>
      <el-form ref="formHandle" :model="formHandle" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="反馈标题" prop="title">
              <el-input v-model="formHandle.title" placeholder="请输入反馈标题" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提交用户" prop="userId">
              <el-select v-model="formHandle.userId" placeholder="请输入选择用户" clearable disabled>
                <el-option
                  v-for="(item, index) in userOptions"
                  :key="index"
                  :value="item.id"
                  :label="item.userName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="反馈状态">
              <el-radio-group v-model="formHandle.status">
                <el-radio
                  v-for="dict in dict.type.sys_feedback_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="反馈内容">
              <div v-html="formHandle.content" class="form-handle-content"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回复" prop="reply">
              <editor-comment v-model="formHandle.reply" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormHandle">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/system/feedback";
import {queryUserSelect} from "@/api/system/baseUser";

export default {
  name: "Feedback",
  dicts: ['sys_feedback_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户反馈表格数据
      feedbackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示处理反馈弹框
      openHandle: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        status: null,
        reply: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 处理弹框表单
      formHandle: {},
      // 表单校验
      rules: {
      },
      // 用户下拉数据
      userOptions: []
    };
  },
  created() {
    this.getUserList();
    this.getList();
  },
  methods: {
    /** 查询用户反馈列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openHandle = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        status: "0",
        reply: null,
        adminId: null,
        userId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
        delFlag: null
      };
      this.formHandle = {
        id: null,
        title: null,
        content: null,
        status: "0",
        reply: null,
        adminId: null,
        userId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
      this.resetForm("formHandle");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length!==1
      this.multiple = !row.records.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户反馈";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交处理表单 */
    submitFormHandle() {
      const data = {
        id: this.formHandle.id,
        status: this.formHandle.status,
        reply: this.formHandle.reply
      }
      updateFeedback(data).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openHandle = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户反馈编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    },
    /** 获取用户列表 */
    getUserList() {
      queryUserSelect([]).then(res => {
        this.userOptions = res.data;
      })
    },
    /** 处理反馈 */
    handleFeedBack(row) {
      this.reset();
      const id = row.id
      getFeedback(id).then(response => {
        this.formHandle = response.data;
        this.openHandle = true;
      });
    }
  }
};
</script>
<style lang="scss">
.form-handle-content {
  width: 100%;
  padding: 15px;
  background-color: #F5F7FA;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
}
</style>
